#ifndef MUTAGRAPHICSVIEW_H
#define MUTAGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QPainterPath>
#include <MutaLine.h>
#include "MutaScene.h"
#include "MutaObject.h"
#include <QList>
#include "MutaEvent.h"
#include "MutaCursor.h"
#include "MutaRepeat.h"
#include "MutaCurve.h"
#include "MutaTendril.h"
#include "MutaStrangeAttractor.h"
#include <QHash>
#include <QReadWriteLock>

#include "Global.h"
//#include "Mutagen.h"

class MutaGraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    MutaGraphicsView( QWidget* parent, MutaScene* scene,MutaCursor *cursor );
    MutaGraphicsView();
    ~MutaGraphicsView();
    void setEditMode(Muta::MutaType editMode);
    void drawUpdate();
    void multiScrollContentsBy(int dx, int dy);
    int getLayer();

private:
    void init();
    QPoint mouseDownPos;
    QPoint lastMouseViewPos;
    QPoint mousePos;
    QGraphicsRectItem* selectionRect;
    QList<QGraphicsItem*> copyItems;
    Muta::MutaType editMode;
    QPointer<MutaLine> tempLine;
    QPointer<MutaEvent> tempEvent;
    QPointer<MutaRepeat> tempRepeat;
    QPointer<MutaCurve> tempCurve;
    QPointer<MutaStrangeAttractor> tempStrangeAttractor;
    QPointer<MutaTendril> tempTendril;
    //QPointer<MutaBlend> tempBlend;
    qreal totalScaleFactor;
    MutaCursor *cursor;
    bool scrollable;
    int layer;
    int beats;
    QHash<QString,MutaObject*> mutaObjects;
    QReadWriteLock mutaObjectsLock;
    bool commandDown;

protected:
    void keyPressEvent( QKeyEvent *event );
    void mouseMoveEvent( QMouseEvent *event );
    void mouseReleaseEvent( QMouseEvent *event );
    void mousePressEvent( QMouseEvent *event );
    void drawBackground(QPainter* painter, const QRectF& rect);
    void drawForeground(QPainter* painter, const QRectF& rect);
    void resizeEvent( QResizeEvent *event );
    void scrollContentsBy(int dx, int dy);
    void keyReleaseEvent(QKeyEvent *event);

signals:
    void zoomChanged(float zoom);
    void scrolling(int dx,int dy);
    void deletingItem(QString objectID);
    void repeatObjectAdded(MutaRepeat* repeat);

    //OSCthulhu
    void addEvent(float x, float y, int layer, float length);
    void addLine(QString type,float x, float y, int layer);
    void removeObject(QString objectID);
    void addedMutaEventToScene(MutaEvent* event);
    void addedMutaLineToScene(MutaLine* mutaLine);
    void addedMutaCurveToScene(MutaCurve* mutaCurve);
    void addedMutaTendrilToScene(MutaTendril* mutaTendril);
    void addedMutaRepeatToScene(MutaRepeat* mutaRepeat);
    //void updateCursorX(QString userName, int x);
    //void updateCursorY(QString userName, int y);
    void cursorPositionChanged(QString userName, QPointF pos);

public slots:
    void addItem(QGraphicsItem* item);
    void setLayer(int layer);
    void setBeats(int beats);
    void updateCursor(qreal delta);
    void addMutaEvent(QString objectID,QString owner,float x, float y, int layer, float length);
    void addMutaLine(QString objectID, QString type,QString owner,float x,float y,int layer,QString controlPoints);
    void addUserCursor(QString objcetID, QString owner);
    void removeMutaObject(QString objectID);
    void setMutaX(QString objectID,float x);
    void setMutaY(QString objectID,float y);
    void setControlPoints(QString objectID,QString controlPointString);
    void userAdded(QString user);
    void userRemoved(QString user);
};

#endif // MUTAGRAPHICSVIEW_H
